// pages/other/eventopen/eventopen.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var click_index = 0;
var app = getApp()
var win_scale = 750 / app.data.deviceInfo.windowWidth;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab_flag: true,
    has_fixed: false,
    event_is_show: true,
    current_index: 0,
    need_common: 1,
    brand_id: 0,
    page: 1,
    page_size: 20,
    event_goods: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenEvent();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initTabObserver();
  },

  viewgoodsDetail: function (e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },

  changeTab: function (e) { // 顶部栏目切换
    const {
      index,
      id
    } = e.currentTarget.dataset;

    if (click_index == index) return

    click_index = index;

    this.setData({
      brand_id: id,
      event_goods: [],
      show_loading: true,
      current_index: index,
      prev_brand_id: id,
      scroll_left: ((index + 1) * 130 - 375 - 65) / win_scale
    })
    if (!this.data.tab_flag) {
      this.switchMenu();
    }
    this.getOpenEvent();
  },

  switchMenu: function (e) { // 遮罩层事件 顶部栏目， 右边下拉按钮切换
    if (this.data.scroll_top == 0) { // 此处为了处理小程序bug
      wx.pageScrollTo({
        scrollTop: 1,
        duration: 0
      })
    }

    this.setData({
      mask_layer: !this.data.mask_layer,
      tab_flag: !this.data.tab_flag
    })
  },

  getOpenEvent: function () {
    var data = {}, that = this;
    data['page'] = this.data.page;
    data['brand_id'] = this.data.brand_id;
    data['page_size'] = this.data.page_size;
    data['need_common'] = this.data.need_common;
    shihan.sendRequest(config.open_event_index, 'POST', data)
      .then(function (response) {
        //sh_is_start 场次状态：0 即将开始 1 正在抢购中 2 已结束
        if (response.data.code == 0) {
          var products = response.data.data.sh_products;
          if (products.length > 0) {
            that.setData({
              is_partner: response.data.args.is_partner,
              event_goods: that.data.event_goods.concat(products),
              has_more: true,
              list_flag: true
            })
            // console.log(that.data.event_goods)
          } else {
            that.setData({
              has_more: false,
              list_flag: false
            })
          }

          if (that.data.event_goods.length == response.data.data.sh_total_rows) {
            that.setData({
              has_more: false,
              list_flag: false
            })
          }
          if (that.data.page == 1 && that.data.need_common == 1) {
            if (response.data.data.sh_brand_list.length > 0) {
              response.data.data.sh_brand_list.unshift({
                'sh_id': 0,
                'sh_brand_name': '全部'
              });
              response.data.data.sh_brand_list.forEach(function(el, index) {
                if (that.data.prev_brand_id == el.sh_id) {
                  that.setData({
                    prev_brand_id: el.sh_id,
                    current_index: index
                  })
                  click_index = index;
                }
              })
            }
            that.setData({
              need_common: 0,
              brand_list: response.data.data.sh_brand_list,
              share_data: response.data.data.sh_share_data,
              banner_img: response.data.data.sh_top_image_url
            })
          }
        }
        wx.stopPullDownRefresh()
        that.setData({
          show_loading: false,
          event_is_show: false
        })
      }, function (error) {
        console.log(error);
      });
  },

  initTabObserver: function () {
    this.tabObserver = wx.createIntersectionObserver(this);
    this.tabObserver.relativeToViewport().observe('.banner', (res) => {
      // console.info(res)
      this.setData({
        has_fixed: res.intersectionRatio > 0 ? false : true
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if (this.tabObserver) this.tabObserver.disconnect();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      event_is_show: true
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      need_common: 1,
      brand_id: '',
      event_goods: [],
      show_loading: true,
      has_more: true,
      list_flag: true
    })
    this.getOpenEvent();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.list_flag) {
      this.setData({
        list_flag: false,
        page: this.data.page + 1
      })
      this.getOpenEvent();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.share_data.sh_program_title,
      path: that.data.share_data.sh_program_url,
      imageUrl: that.data.share_data.sh_program_image,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})